package com.example.demo.demo;

import com.alibaba.fastjson.JSONObject;
import com.young.demo.domain.TaskDBParam;
import com.young.domain.CallBaseParam;
import com.young.domain.DataBaseQueryParam;
import com.young.domain.RunTaskResult;
import com.young.task.AsynTaskOperatorUtil;
import com.young.thread.CommonThreadPoolBuildUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @Description 第三方系统推送任务
 * @Author kangyang
 * @Version 1.0
 * @Date 2024/07/22 13:33
 */
@Component
public class ThirdPartyPushTask {

    private static final Logger log = LoggerFactory.getLogger(CommonThreadPoolBuildUtils.class);

    public RunTaskResult execute(JSONObject params) {
        log.info("ThirdPartyPushTask.start:{}", params);
        DataBaseQueryParam dataParam = JSONObject.parseObject(params.toJSONString(), DataBaseQueryParam.class);
        return AsynTaskOperatorUtil.applyOperator(param -> {
            log.info("ThirdPartyPushTask.apply param{}", JSONObject.toJSONString(param));
            return assembleTaskDBParam();
        }, this.getClass(), PushAsyncTask.class, new CallBaseParam<TaskDBParam>(), dataParam);
    }

    private List<TaskDBParam> assembleTaskDBParam() {
        List<TaskDBParam> taskDBParamList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TaskDBParam taskDBParam = new TaskDBParam();
            taskDBParam.setDbName("test" + i);
            taskDBParam.setDbType(i);
            taskDBParam.setTableName("test" + i);
            taskDBParamList.add(taskDBParam);
        }
        return taskDBParamList;
    }

    public static class PushAsyncTask implements Callable<Boolean> {

        CallBaseParam<TaskDBParam> callBaseParam;

        public PushAsyncTask(CallBaseParam callBaseParam) {
            this.callBaseParam = callBaseParam;
        }

        @Override
        public Boolean call() throws Exception {
            if (null == callBaseParam || CollectionUtils.isEmpty(callBaseParam.getList())) {
                return false;
            }
            log.info("ThirdPartyPushTask.PushAsyncTask.call.start.param:{}", JSONObject.toJSONString(callBaseParam));
            return true;
        }
    }
}
